<?php

namespace app\adminmall\controller;
use app\adminmall\model\LuckyDrawModel;
use app\web\model\GoodsTypeModel;
use app\web\model\StoreGoodsTypeModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 抽奖交友团列表
 */
class LuckyDrawController extends AdminBaseController
{
 	protected $model;
	protected $goodsType_model;
	protected $storeGoodsType_model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new LuckyDrawModel();
		$this->goodsType_model = new GoodsTypeModel();
		$this->storeGoodsType_model = new StoreGoodsTypeModel();
		
		//商品类目
		$goodsType_list = $this->goodsType_model->select();
		//商品分类
		$storeGoodsType_list = $this->storeGoodsType_model->select();
		$this->assign('storeGoodsType_list',$storeGoodsType_list);
		$this->assign('goodsType_list',$goodsType_list);		
    }
	
	
    /**
     * 商品列表
	 * @param int $page
     */
    public function index($page = 1){
    	$field = 'm.*,g.store_name';
		$map = [];
		if(request()->isPost()){
		 	$param = input('post.');
		 	if($param['goodsname']){
			$map['name'] = ['like','%'.$param['goodsname'].'%'];			
		}
		if($param['store_name']){
			$map['store_name'] = ['like','%'.$param['store_name'].'%'];			
		}
		 //创建时间
			if($param['add_time']&&$param['end_time']){
				$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}}
		$list = Db::name("mall_goods_pintuan")->alias('m')->join('mall_agent g','g.id=m.agent_id')->where($map)->field($field)->order("m.add_time desc")->paginate(10);
		return $this->fetch('index', ['list' => $list]);
    }
	
	/**
     *添加抽奖交友团商品
     */
    public function add(){
        if(request()->isAjax()){
        	$param = input('post.');
			$param['add_time'] = now_time();
			$param['agent_id'] = session('storeInfo')['id'];
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('lucky_draw/index'));
            }else{
                $this->error('添加失败');
            }
        }
		 return $this->fetch();
    }

    /**
     * [edit 编辑]
     * @return 
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('lucky_draw/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
		$this->assign('pid','');
        return $this->fetch();
    }
	
    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}